package com.myFwk.threadPool;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @Title:
 * @Description: 全局线程池，实现通过线程池的方式，实现动态的配置线程的各个参数
 * @Author: guowl
 * @version： 1.0
 * @Date:2021/10/15
 * @Copyright: Copyright(c)2021 RedaFlight.com All Rights Reserved
 */

@Configuration
public class CustomThreadPool {

    /**
     * 最大核心线程数
     */
    @Value("${customthread.corePoolSize:1}")
    private String corePoolSize;
    /**
     * 最大线程数
     */
    @Value("${customthread.maximumPoolSize:1}")
    private String maximumPoolSize;
    /**
     * 队列最大数
     */
    @Value("${customthread.queueMax:100}")
    private String queueMax;

    /**
     * 全局自定义线程池 保存在容器当中
     * @return
     */
    @Bean(name = "threadPoolExecutor")
    public ThreadPoolExecutor getTheadPool() {
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
                Integer.valueOf(corePoolSize),
                Integer.valueOf(maximumPoolSize),
                Long.valueOf(1),
                TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(Integer.valueOf(queueMax)),
                new ThreadPoolExecutor.AbortPolicy()
        );
        return threadPoolExecutor;
    }
}
